import * as lib from '../../../../components/aotoo/lib/index';
import {getCaseIdByName} from '../../funcs/index'

const dateInfoHooks = lib.hooks('date-info', true)
export let recentReciprocal = null

export function emptyRecentReciprocal(){
  recentReciprocal = null
}

export function getRecentReciprocal(){
  return recentReciprocal
}

export const setPanReciprocal = (ctx) => {
  const reciprocalList = dateInfoHooks.getItem('case-reciprocalList')
  const reciprocalInst = ctx.getElementsById('date-info-reciprocal') 
  if (!reciprocalInst) return
  const id = getCaseIdByName('reciprocal') || 22

  if (reciprocalList.length) {
    const filterTemp = reciprocalList.filter(item=>item.targetDay.diffDays>=0)
    const temp = filterTemp.length ? filterTemp.sort( (a,b)=> (a.targetDay.diffDays - b.targetDay.diffDays) )[0] : null
    
    if (temp) {
      if (recentReciprocal && recentReciprocal.targetDay.timestr === temp.targetDay.timestr) return
      else {
        recentReciprocal = temp
      }
      if (reciprocalInst) {
        reciprocalInst.removeClass('invalid')
        if (recentReciprocal.targetDay.diffDays === 0) {
          reciprocalInst.update({body: [
            {title: lib.subcontent((recentReciprocal.body[2]||'未填写'), 8, true), itemStyle: 'font-size: 22px;'},
          ]})
        } else {
          reciprocalInst.update({body: [
            ""+recentReciprocal.targetDay.diffDays,
            lib.subcontent((recentReciprocal.body[2]||'未填写'), 8, true),
            '倒数日'
          ]})
        }
      }
    } else {
      reciprocalInst.addClass('invalid')
      reciprocalInst.update({
        body: [
          ' ',
          {title: '倒数日', attr: {type: id}, aim: 'onCase', itemStyle: 'margin-top: -2em; padding-top: 2em'}
        ]
      })
    }
  } else {
    reciprocalInst.addClass('invalid')
    reciprocalInst.update({
      body: [
        ' ',
        {title: '倒数日', attr: {type: id}, aim: 'onCase', itemStyle: 'margin-top: -2em; padding-top: 2em'}
      ]
    })
  }
}